home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11298 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Compiler unable to comprehend template for "<<" operator. What's wrong???
  5. Message-ID: <marnoldDo3BBI.5Hw@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4hto1l$bvl@newshost.lanl.gov>
  8. Date: Mon, 11 Mar 1996 06:28:30 GMT
  9. Sender: marnold@netcom8.netcom.com
  10.  
  11. staley@canary.lanl.gov (Martin Staley) writes:
  12.  
  13.  
  14. >Consider the following code:
  15.  
  16. >   #include <iostream.h>
  17.  
  18. >   template<class Type>
  19. >      class vector {
  20. >         Type *v;
  21. >         int   size;
  22. >         friend ostream &operator<<(ostream &, vector<Type> &);
  23. >      };
  24.  
  25. >   template<class Type>
  26. >      ostream &operator<<(ostream &ostr, vector<Type> &vec) {
  27. >      }
  28.  
  29. >   main() {
  30. >      vector<float> v;
  31. >      cout << v;
  32. >   }
  33.  
  34. >This is an extremely simplified version of some stuff I'm writing.  But
  35. >it will not compile.  Both g++ (for the Suns) and Sun c++ give me the
  36. >following error:
  37.  
  38. >   ld: Undefined symbol 
  39. >      ___ls__FR7ostreamRt6vector1Zf
  40.  
  41. >Apparently the compiler doesn't know how to use the template for
  42. >operator<< to instantiate the << operator for a vector<float>.  WHAT'S
  43. >WRONG HERE???  I've tried several minor variations to the above code,
  44. [snip]
  45. >Again, the compiler seems unable to build the necessary operator<<.
  46. >Things work fine if I write operator<< specifically for floats:
  47. [snip]
  48. >Can anybody tell me what, if anything, that I am doing wrong above?
  49.  
  50. Nothing.
  51.  
  52. Apparently, the compiler you are using doesn't work.  Your code compiles 
  53. and runs *as-is* (I just cut and pasted it) without errors under Borland 
  54. C++.
  55.  
  56. As a work-around, I guess you *will* have to explicity write each 
  57. variation of your friend operator<<() function until your compiler vendor
  58. is inclined to properly support the langauge.  C'est la vie.  :-(
  59.  
  60. Report your code to the vendor as a clear demonstration of a language 
  61. support bug.
  62.  
  63. Regards,
  64. -------------------------------------------------------------------------
  65. Matt Arnold                       |        | ||| | |||| |  | | || ||
  66. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  67. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  68. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  69. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  70. -------------------------------------------------------------------------
  71.  
  72.